﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
namespace HZ.DrugTrade.Web.Admin.SystemManage
{
    public partial class SortManage : BasePage
    {
        private void Page_Load(object sender, System.EventArgs e)
        {

            if (this.Page.Request.QueryString["ID_ID"] != string.Empty && this.Page.Request.QueryString["ID_ID"] != null)
            {
                this.McID = int.Parse(this.Page.Request.QueryString["ID_ID"].ToString());
                //展开指定接点
                DealExpand(this.tvMtlCategory.Nodes, this.McID.ToString());
            }

            if (!this.IsPostBack)
            {
                //装载类别树信息
                LoadMtlCategroy();
            }
        }


        /// <summary>
        /// 默认展开的McID
        /// </summary>
        private int McID = -1;
      

        /// <summary>
        /// 装载类别树信息
        /// </summary>
        private void LoadMtlCategroy()
        {
           // DataView table = MtlCategoryController.GetMaterialCategoryList(0);

            //调用生成树的递归方法
            CreateTree(this.tvMtlCategory.Nodes, "0");


        }

        /// <summary>
        /// 递归加载树的方法
        /// </summary>
        /// <param name="tnc">节点集合</param>
        /// <param name="iPID">父节点编号ID </param>
        private void CreateTree(TreeNodeCollection tnc, string iPID)
        {
            IBLL_Sort ibbs = Factory.IBLL_Sort();

            IList<Sort> Lsort = ibbs.GetAllSort(iPID);

            for (int i = 0; i < Lsort.Count; i++)
            {
                Sort category = (Sort)Lsort[i];

                TreeNode node = new TreeNode();

                string strUrl = "SortEditManage.aspx?ID_ID=" + category.SortID.ToString() + "&IDCount=" + Lsort.Count.ToString() + "&IDson="+GetSon(category.SortID);
            
                 node.Text = GetHrefText("frmDictionary", strUrl, category.SortName);
                 node.Value = category.SortID.ToString(); 
              
               //  node.ImageUrl = "../../lib/images/add.gif";

                 tnc.Add(node);

               //  CreateTree(node.ChildNodes, category.SortID);
            }
        }

        /// <summary>
        /// 展开指定接点
        /// </summary>
        /// <param name="tcn"></param>
        /// <param name="iMcID"></param>
        public void DealExpand(TreeNodeCollection tnc, string iMcID)
        {
            foreach (TreeNode node in tnc)
            {
                if (node.Value == iMcID.ToString())
                {
                    node.Expanded = true;
                    break;
                }

                DealExpand(node.ChildNodes, iMcID);
            }
        }

        /// <summary>
        /// 根据Url和要显示的名称组合成可用的树节点文本
        /// </summary>
        /// <param name="frameName"></param>
        /// <param name="strURL"></param>
        /// <param name="strDispalyName"></param>
        /// <returns></returns>
        private string GetHrefText(string frameName, string strURL, string strDispalyName)
        {
            return "<A  style=\"TEXT-DECORATION: none; COLOR: black \"  href=\"javascript:FreshFrame('" + frameName + "','" + strURL + "')\">" + strDispalyName + "</A>";
        }

        protected void tvMtlCategory_SelectedNodeChanged(object sender, EventArgs e)
        {
                CreateTree(tvMtlCategory.SelectedNode.ChildNodes, tvMtlCategory.SelectedNode.Value);
                this.tvMtlCategory.SelectedNode.ExpandAll();

        }
        /// <summary>
        /// 获取当前节点子节点个数
        /// </summary>
        /// <param name="strid"></param>
        /// <returns></returns>
        private string  GetSon( string strid)
        {
            IBLL_Sort ibbs = Factory.IBLL_Sort();

            IList<Sort> Lsort = ibbs.GetAllSort(strid);
            return Lsort.Count.ToString();
        }
    }
}
